package com.douwong.student.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.activity.ImageActivity;
import com.douwong.student.model.ClassFile;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class ClassFileInnerAdapter extends BaseAdapter {

	private ArrayList<ClassFile>list;
	private Context context;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	public ClassFileInnerAdapter(ArrayList<ClassFile>list, Context context) {
		this.list=list;
		this.context=context;
		options= new DisplayImageOptions.Builder()
				.showImageOnLoading(R.mipmap.pic_1)
				.showImageForEmptyUri(R.mipmap.pic_1)
				.showImageOnFail(R.mipmap.pic_1)
				.cacheInMemory(true)
				.cacheOnDisk(true)
				.considerExifParams(true)
				.displayer(new FadeInBitmapDisplayer(500))
				.build();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder=null;
		if(convertView==null){
			holder=new Holder();
			convertView=LayoutInflater.from(context).inflate(R.layout.pic_item, null);
			holder.pic=(ImageView) convertView.findViewById(R.id.pic);
			holder.subject=(TextView) convertView.findViewById(R.id.subject);
			convertView.setTag(holder);
		}else{
			holder=(Holder) convertView.getTag();
		}
		if(list.size()>0){
			try {
				holder.subject.setText(list.get(position).getSujectName());
				String url=list.get(position).getResInfoBean().getResExt().getThuAddress();
				ImageLoader.getInstance().displayImage(url, holder.pic, options, animateFirstListener);
				holder.pic.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent=new Intent();
						ArrayList<String> imgList=new ArrayList<String>();
						for(int i=0;i<list.size();i++){
							String img=list.get(i).getResInfoBean().getResExt().getDownloadUrl();
							if(img!=null){
								imgList.add(img);
							}
						}
						intent.setClass(context, ImageActivity.class);
						intent.putStringArrayListExtra("imgs", imgList);
						intent.putExtra("position",position);
						context.startActivity(intent);
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return convertView;
	}
	class Holder {
		ImageView pic;
		TextView subject;
	}
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
